package chuangyi.com.org.DOMIHome.wxapi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.PopupActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.WxPayInfoModel;
import chuangyi.com.org.ViewModel.PayModel;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    public static String WX_ORDER_INFOR = "wx_orderinfo";
    public static String WX_TYPE = "wx_type";
    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    private WxPayInfoModel wxModel;
    private int wx_type;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, "wx7de552bfed09ba2b");
        api.handleIntent(getIntent(), this);
    }

    /**
     * 调用微信支付
     */
    public void wxPayDate() {
        PayReq req = new PayReq();
        //req.appId = "wxf8b4f85f3a794e77";  // 测试用appId
        req.appId = wxModel.getAppid();
        req.partnerId = wxModel.getPartnerid();
        req.prepayId = wxModel.getPrepayid();
        req.nonceStr = wxModel.getNoncestr();
        req.timeStamp = wxModel.getTimestamp();
        req.packageValue = wxModel.getPackage();
        req.sign = wxModel.getSign();
//		req.extData			= "app data"; // optional
        Toast.makeText(this, "正常调起支付", Toast.LENGTH_SHORT).show();
        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
        api.sendReq(req);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @SuppressLint("NewApi")
    @Override
    public void onResp(BaseResp resp) {
        Log.e(TAG, "onPayFinish, errCode = " + resp.errCode);

        if (resp.errCode == 0) {
            Toast.makeText(WXPayEntryActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
//			finishFromChild(WXPayEntryActivity.this);
            if (wx_type == 1) {
                Intent intent = new Intent(WXPayEntryActivity.this, HomeActivity.class);
                startActivity(intent);
                finishAffinity();
            } else {
                finishFromChild(WXPayEntryActivity.this);
            }
            PopupActivity.notifyPayResult(true);
        } else if (resp.errCode == -1) {

            Toast.makeText(WXPayEntryActivity.this, resp.errStr, Toast.LENGTH_SHORT).show();
            PopupActivity.notifyPayResult(false);
            finishFromChild(WXPayEntryActivity.this);
        } else if (resp.errCode == -2) {
            Toast.makeText(WXPayEntryActivity.this, "取消支付", Toast.LENGTH_SHORT).show();
            PopupActivity.notifyPayResult(null);
            finish();
        }
    }
}